﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace Adams.Admin.DataAccess.Events
{
    public static class Calendars
    {
        public static Adams.Admin.DataTransfer.Events.Calendars.PageLoad PageLoad()
        {
            var data = new Adams.Admin.DataTransfer.Events.Calendars.PageLoad();
            

            using (DataSet ds = DB.RunDataSet(@"
SELECT * FROM Events.Calendars
", CommandType.Text))
            {
                data.Calendars = new Shared.Entities.Events.CalendarCollection();
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    var calendar = new Shared.Entities.Events.Calendar();
                    calendar.ID = dr["CalendarID"].DbInt();
                    calendar.Name = dr["Name"].DbString();
                    calendar.Description = dr["Description"].DbString();
                    calendar.Color = dr["Color"].DbString();
                    data.Calendars.Add(calendar);
                }
            }

            return data;
        }

        public static void Delete(int id)
        {
            DB.Run(@"
DELETE Events.Occurances
WHERE EventID IN (SELECT EventID FROM Events.Events WHERE CalendarID = @CalendarID)

DELETE Events.Events
WHERE CalendarID = @CalendarID

DELETE Events.Calendars
WHERE CalendarID = @CalendarID
", CommandType.Text,
                    DB.CreateParameter("@CalendarID", id, SqlDbType.Int));
        }
    }
}
